package cn.tedu._02article.controller;

import cn.tedu._02article.base.response.JsonResult;
import cn.tedu._02article.base.response.StatusCode;
import cn.tedu._02article.mapper.UserMapper;
import cn.tedu._02article.pojo.dto.UserArticleParam;
import cn.tedu._02article.pojo.dto.UserLoginParam;
import cn.tedu._02article.pojo.entity.Article;
import cn.tedu._02article.pojo.vo.ArticleVO;
import cn.tedu._02article.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Validated
@RestController
@RequestMapping("v1/user/")
@Tag(name = "用户管理模块")
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult userLogin(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("开始处理【用户登录】的请求，参数：{}",userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsernameAndPassword(username,  password);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }

    @PostMapping("article")
    @Operation(summary = "发布文章")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@Validated @RequestBody UserArticleParam userArticleParam, HttpSession session){
        log.debug("【发布文章】的请求参数：{}",userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam,article);
        article.setUserId(userId);
        log.debug("即将添加文章：{}",article);
        int result = userMapper.addArticle(article);
        if (result > 0){
            return JsonResult.ok();
        }
        return JsonResult.fail(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("/selectByArticleId")
    @Operation(summary = "根据文章id查询文章")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByArticleId(@Range(min = 1,max = 100, message = "文章ID要求在1-100之间") Long id, HttpSession session){
        log.debug("开始处理【根据文章id查询文章】的请求，参数：{}" + id);
        ArticleVO articleVO = userMapper.selectArticleById(id);
        if (articleVO == null){
            return new JsonResult(StatusCode.NOT_FOUND);
        }
        return JsonResult.ok(articleVO);
    }
}


